package com.wutuo.wtservice.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.util.CommonUtil;
import com.wutuo.wtservice.api.WtTypeAPIService;
import com.wutuo.wtservice.api.request.ListWtTypeRequest;
import com.wutuo.wtservice.api.request.WtTypeRequest;
import com.wutuo.wtservice.api.response.WtTypeResponse;
import com.wutuo.wtservice.provider.dao.WtTypeMapper;
import com.wutuo.wtservice.provider.domain.WtType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 标签关系表 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service public class WtTypeService extends BaseService<WtType, Long> implements WtTypeAPIService {

    private Logger logger = LoggerFactory.getLogger(WtTypeService.class);

    @Autowired private WtTypeMapper wtTypeMapper;

    @Override protected GenericMapper<WtType, Long> getMapper() {

        // TODO Auto-generated method stub
        return wtTypeMapper;
    }

    public void addorUpdate(WtTypeRequest record) {

        if (record.getId() == null) {
            if (list(record).size() == 0) {
                super.add(record);
            }
        } else {
            super.update(record);
        }
    }

    public void del(Long id) {

        WtTypeRequest model = new WtTypeRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<WtTypeResponse> lisForPage(ListWtTypeRequest request) {

        List<WtTypeResponse> list = wtTypeMapper.lisForPage(request);
        return list;
    }

    public List<WtTypeResponse> list(WtTypeRequest request) {

        request.setInitFlag(true);
        request.setDelFlag(true);
        return wtTypeMapper.list(request);
    }

    /**
     * 翻译TAG
     *
     * @param model
     * @param tKey
     * @param skey
     * @return
     */
    public <T> List getWtTypeByIds(List<T> model, String tKey, String skey) {

        List<WtTypeResponse> listTag = list(new WtTypeRequest());
        Map map = CommonUtil.listToObject(listTag, "id");
        for (int i = 0; i < model.size(); i++) {
            Object modelS = model.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            if (map.containsKey(id)) {
                CommonUtil.setProperty(modelS, skey, ((WtTypeResponse) map.get(id)).getTypeImage());
                CommonUtil.setProperty(modelS, "sort", ((WtTypeResponse) map.get(id)).getRemarks());
            }
        }
        return model;
    }

}